package org.yxing.palpalgps.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.jivesoftware.smackx.packet.DataForm;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.models.RecentChatModel;
import org.yxing.palpalgps.utils.Helper;
import org.yxing.palpalgps.utils.MyBase64;

import android.content.Context;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RecentChatsAdapter extends BaseAdapter{
    
    private ArrayList<RecentChatModel> data;
    private Context mContext;
    private AsyncImageLoader mAsyncImageLoader;
    private SimpleDateFormat dateFormat;
    
    public RecentChatsAdapter (Context context) {
        mContext = context;
        data = new ArrayList<RecentChatModel>();
        mAsyncImageLoader = new AsyncImageLoader();
        dateFormat = new SimpleDateFormat("MM-dd HH:mm");
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public RecentChatModel getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RecentChatModel model = getItem(position);
        convertView = LayoutInflater.from(mContext).inflate(R.layout.recent_chats_item, null);
        //portrait
        ImageView imageViewPortrait = (ImageView) convertView.findViewById(R.id.imageViewPortrait);
        if (!TextUtils.isEmpty(model.getPortrait()))
            mAsyncImageLoader.loadDrawable(Helper.getSmallPictureUrl(model.getPortrait()), imageViewPortrait);
        //set name
        TextView textViewName = (TextView) convertView.findViewById(R.id.textViewName);
        textViewName.setText(model.getFname());
        //set quantity
        TextView textViewQuantity = (TextView) convertView.findViewById(R.id.textViewQuantity);
        textViewQuantity.setText(Integer.toString(model.getNum()));
        //set last message
        TextView textViewLastMsg = (TextView) convertView.findViewById(R.id.textViewLastMsg);
        textViewLastMsg.setText(MyBase64.decode(model.getLastMsg()));
        //set last time
        TextView textViewLastTime = (TextView) convertView.findViewById(R.id.textViewLastTime);
        textViewLastTime.setText(dateFormat.format(new Date(model.getLastTimestamp())));
        
        
        
        return convertView;
    }
    
    /**
     * add new record
     * @param model
     */
    public void add (RecentChatModel model) {
        data.add(model);
        notifyDataSetChanged();
    }
    
    public void clear() {
        data.clear();
    }

}
